package com.authine.cloudpivot.web.api.quartz.task;

import com.alibaba.dubbo.config.annotation.Reference;
import com.authine.cloudpivot.engine.api.facade.TaskReportFacade;
import com.authine.cloudpivot.web.api.dao.UserMapper;
import com.authine.cloudpivot.web.api.service.impl.FescoadQueryServiceImpl;
import com.authine.cloudpivot.web.api.vo.User;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class RemindNoContractJob  implements Job {

    @Value("${projectDirectorRoleId}")
    private String projectDirectorRoleId;

    @Value("${executeDirectorId}")
    private String executeDirectorId;


    @Value("${biDirectorId}")
    private String biDirectorId;

    @Value("${generalManagerId}")
    private String generalManagerId;

    @Reference(version = "1.00")
    private TaskReportFacade taskReportFacade;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FescoadQueryServiceImpl fescoadQueryService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("无合同立项提醒开始------------------------------------");
        JobDataMap jobDataMap = context.getMergedJobDataMap();
        int remind =  (Integer)jobDataMap.get("data1");
        int warn =  (Integer)jobDataMap.get("data2");
        int alarm =  (Integer)jobDataMap.get("data3");
        int num = (Integer)jobDataMap.get("data4");

        if(num == 1){
            List<Map<String, Object>> remindResultMapList  =  taskReportFacade.getNoContractProject((warn-1) * 7,(remind-1)+1);
            remindResultMapList.forEach(
                    item->{
                        String projectNames  =  item.get("projectNames").toString();
                        String subjectPerson = item.get("subjectPerson").toString();
                        log.info(userMapper.getUserId(subjectPerson).getName()+"【提醒】处于无合同立项状态的【"+projectNames+"】项目还未补交合同，请尽快补交！");
                        fescoadQueryService.sendTextMessage(subjectPerson,"","【提醒】处于无合同立项状态的【"+projectNames+"】项目还未补交合同，请尽快补交！","无合同立项提醒");
                    }
            );

        }else if(num == 2){

            List<Map<String, Object>> warnResultMapList1  =  taskReportFacade.getNoContractProject((alarm-1) * 7,(warn-1) * 7);

            List<Map<String, Object>> warnResultMapList2  =  taskReportFacade.getNoContractProject(60,(alarm-1) * 7);

            warnResultMapList1.forEach(
                    item->{
                        String projectNames  =  item.get("projectNames").toString();
                        String subjectPerson = item.get("subjectPerson").toString();
                        int days = 60 - Integer.parseInt(item.get("days").toString());
                        String projectDirectorId = taskReportFacade.getProjectDirector(subjectPerson, projectDirectorRoleId);
                        log.info(userMapper.getUserId(subjectPerson).getName()+"【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！");
                        fescoadQueryService.sendTextMessage(subjectPerson,"","【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！","无合同立项预警");

                        log.info(userMapper.getUserId(projectDirectorId).getName()+"【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！");
                        fescoadQueryService.sendTextMessage(projectDirectorId,"","【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！","无合同立项预警");
                    }
            );

            warnResultMapList2.forEach(
                    item->{
                        String projectNames  =  item.get("projectNames").toString();
                        String subjectPerson = item.get("subjectPerson").toString();
                        int days = 60 - Integer.parseInt(item.get("days").toString());
                        String projectDirectorId = taskReportFacade.getProjectDirector(subjectPerson, projectDirectorRoleId);
                        log.info(userMapper.getUserId(subjectPerson).getName()+"【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！");
                        fescoadQueryService.sendTextMessage(subjectPerson,"","【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！","无合同立项预警");

                        log.info(userMapper.getUserId(projectDirectorId).getName()+"【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！");
                        fescoadQueryService.sendTextMessage(projectDirectorId,"","【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！","无合同立项预警");

                        if(item.get("operator") != null){
                            List<String> operators = (List<String>)item.get("operator");
                            for(String operator : operators){
                                log.info(userMapper.getUserId(operator).getName()+"【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！");
                                fescoadQueryService.sendTextMessage(operator,"","【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！","无合同立项预警");
                            }
                        }

                        log.info(userMapper.getUserId(executeDirectorId).getName()+"【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！");
                        fescoadQueryService.sendTextMessage(executeDirectorId,"","【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！","无合同立项预警");

                        log.info(userMapper.getUserId(biDirectorId).getName()+"【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！");
                        fescoadQueryService.sendTextMessage(biDirectorId,"","【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！","无合同立项预警");

                        log.info(userMapper.getUserId(generalManagerId).getName()+"【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！");
                        fescoadQueryService.sendTextMessage(generalManagerId,"","【预警】处于无合同立项状态的【"+projectNames+"】项目距离60天合同补交期限还剩"+days+"天，需尽快处理！","无合同立项预警");

                    }
            );

        }else if(num == 3){
            List<Map<String, Object>> alarmResultMapList  =  taskReportFacade.getNoContractProject(99999,60);

            alarmResultMapList.forEach(
                    item->{
                        String projectNames  =  item.get("projectNames").toString();
                        String subjectPerson = item.get("subjectPerson").toString();
                        String projectDirectorId = taskReportFacade.getProjectDirector(subjectPerson, projectDirectorRoleId);
                        log.info(userMapper.getUserId(subjectPerson).getName()+"【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！");
                        fescoadQueryService.sendTextMessage(subjectPerson,"","【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！","无合同立告警");

                        log.info(userMapper.getUserId(projectDirectorId).getName()+"【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！");
                        fescoadQueryService.sendTextMessage(projectDirectorId,"","【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！","无合同立告警");

                        if(item.get("operator") != null){
                            List<String> operators = (List<String>)item.get("operator");
                            for(String operator : operators){
                                log.info(userMapper.getUserId(operator).getName()+"【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！");
                                fescoadQueryService.sendTextMessage(operator,"","【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！","无合同立告警");
                            }
                        }

                        log.info(userMapper.getUserId(executeDirectorId).getName()+"【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！");
                        fescoadQueryService.sendTextMessage(executeDirectorId,"","【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！","无合同立告警");

                        log.info(userMapper.getUserId(biDirectorId).getName()+"【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！");
                        fescoadQueryService.sendTextMessage(biDirectorId,"","【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！","无合同立告警");

                        log.info(userMapper.getUserId(generalManagerId).getName()+"【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！");
                        fescoadQueryService.sendTextMessage(generalManagerId,"","【告警】处于无合同立项状态的【"+projectNames+"】项目已超过60天合同补交时限，需立刻处理！","无合同立告警");
                    }
            );

        }
        log.info("无合同立项提醒结束------------------------------------");
    }
}